<?php
// Module Body

function pop3_user_update(&$data) {
	global $cfg;
	
	// if username changed, rename POP3 spool file
	if ($data['username'] != $data['username1']) {
		$oldpop3path = $cfg['mail_spool']."/".$data['username1'];
		$newpop3path = $cfg['mail_spool']."/".$data['username'];
		if (file_exists($oldpop3path))
			rename($oldpop3path,$newpop3path);
	}
}

function pop3_user_remove(&$data) {
	global $cfg;

	// Remove POP3 spool file
	$pop3path = $cfg['mail_spool']."/".$data['username'];
	if (file_exists($pop3path) AND $data['username']) {
		unlink($pop3path);
	}
}

function pop3_monitor(&$data) {
	global $cfg;
	if (file_exists($cfg['spid']['pop3'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['pop3'])));
		if (posix_kill($pid,0))
			$data['pop3'] = 'up';
		else {
			$data['pop3'] = 'down';
		}
	} else {
		$data['pop3'] = 'down';	
	}
	if ($data['pop3'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['pop3']) {
			webcp_log(2,0,"monitor","service down: pop3 - attempting to start",0,$echo);
			pop3_start();
			sleep(2);
			if (file_exists($cfg['spid']['pop3'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['pop3'])));
				if (posix_kill($pid,0))
					$data['pop3'] = 'up';
				else {
					$data['pop3'] = 'down';
				}
			} else {
				$data['pop3'] = 'down';	
			}
		}
	}
	if ($data['pop3'] == 'down') {
		webcp_log(1,0,"monitor","service down: pop3",0,$echo);
	}
	return true;
}

// Add cfg vars that relate to pop3
function pop3_add_cfg(&$cfg) {
	if ($cfg['spid']['pop3'] == '/var/run/inetd.pid' && $cfg['init']['pop3'] == '/etc/init.d/inetd') {
		$cfg['spid']['pop3']	= '/var/run/inetd.pid';
		$cfg['init']['pop3']	= '/etc/init.d/inetd';
	}
	$cfg['autostart']['pop3'] = false;
}

// Start the pop3 server
function pop3_start() {
	global $cfg;
	exec($cfg['init']['pop3'].' start');
}

// Stop the pop3 server
function pop3_stop() {
	global $cfg;
	exec($cfg['init']['pop3'].' stop');
}

function pop3_restart() {
	global $cfg;
	exec($cfg['init']['pop3'].' restart');
}
?>